package com.aaa.service.Impl;

import com.aaa.entity.Melody;
import com.aaa.entity.Singer;
import com.aaa.mapper.MelodyMapper;
import com.aaa.mapper.SingerMapper;
import com.aaa.service.MelodyService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.MelodypageInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MelodyServiceImpl extends ServiceImpl<MelodyMapper,Melody> implements MelodyService {

    @Autowired
    private MelodyMapper melodyMapperl;

    @Autowired
    private SingerMapper singerMapper;

    //保存图片的文件地址
    public static final String IMG_PATH="D:/upload/";
    @Override
    public MelodypageInfo<Melody> page(Integer currentPage, Integer pageSize, String pramykey) {
        MelodypageInfo pageinfo = new MelodypageInfo();
        Integer offset = (currentPage-1)*pageSize;
        List<Melody> list = melodyMapperl.listMyMelody(offset,pageSize,pramykey);
        pageinfo.setData(list);
        List<Singer> singers = singerMapper.listMySinger();
        Integer count = melodyMapperl.count(pramykey);
        pageinfo.setCount(count);
        Integer LastPage =count/pageSize;
        if(LastPage%pageSize!=0){
            LastPage++;
        }
        pageinfo.setLastPage(LastPage);
        pageinfo.setCurrentPage(currentPage);
        return pageinfo;
    }

    @Override
    public DefaultMsg onSave(Melody melody) {
        DefaultMsg defaultMsg = new DefaultMsg();
        int i = melodyMapperl.onSave(melody);
        Melody mid = melodyMapperl.SelectMid(melody);
        melody.setMid(mid.getMid());
        int i1 = melodyMapperl.onSaveS_M(melody);
        if(i!=1||i1!=1){
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("保存异常");
        }
        return defaultMsg;
    }

    @Override
    public DefaultMsg MelodyDelete(Integer mid) {
        DefaultMsg defaultMsg = new DefaultMsg();
        int i = melodyMapperl.MelodyDelete(mid);
        int i1 = melodyMapperl.DeleteS_M(mid);
        if(i!=1||i1!=1){
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("删除异常");
        }
        return defaultMsg;
    }
}
